package repository

import (
	"dataaysn/internal/model"
	"time"

	"gorm.io/gorm"
)

type LogDrgDipRepository interface {
	Create(log *model.LogDrgDip) error
	GetByID(id uint) (*model.LogDrgDip, error)
	ListByPatient(patientNo string) ([]model.LogDrgDip, error)
	ListByHospital(hospitalCode string, startDate, endDate time.Time) ([]model.LogDrgDip, error)
}

type GormLogDrgDipRepository struct {
	db *gorm.DB
}

func NewGormLogDrgDipRepository(db *gorm.DB) LogDrgDipRepository {
	return &GormLogDrgDipRepository{db: db}
}

func (r *GormLogDrgDipRepository) Create(log *model.LogDrgDip) error {
	return r.db.Create(log).Error
}

func (r *GormLogDrgDipRepository) GetByID(id uint) (*model.LogDrgDip, error) {
	var log model.LogDrgDip
	err := r.db.First(&log, id).Error
	return &log, err
}

func (r *GormLogDrgDipRepository) ListByPatient(patientNo string) ([]model.LogDrgDip, error) {
	var logs []model.LogDrgDip
	err := r.db.Where("patient_no = ?", patientNo).Find(&logs).Error
	return logs, err
}

func (r *GormLogDrgDipRepository) ListByHospital(hospitalCode string, startDate, endDate time.Time) ([]model.LogDrgDip, error) {
	var logs []model.LogDrgDip
	err := r.db.Where("hospital_code = ? AND create_time BETWEEN ? AND ?",
		hospitalCode, startDate, endDate).Find(&logs).Error
	return logs, err
}
